package 高效制胜._05;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/9/11 8:40
 * 有效的括号
 * 算法：栈
 */
public class _20 {
    public static void main(String[] args) {
        System.out.println(new _20().isValid("(]"));
    }

    public boolean isValid(String s) {
        Deque<Character> stack = new LinkedList<>();
        for (char c : s.toCharArray()) {
            if (stack.isEmpty()) stack.push(c);
            else {
                final Character peek = stack.peek();
                if (peek == '(' && c == ')') stack.pop();
                else if (peek == '[' && c == ']') stack.pop();
                else if (peek == '{' && c == '}') stack.pop();
                else stack.push(c);
            }
        }
        return stack.isEmpty();
    }

}
